package com.loan.controller;
import java.util.Date;
import java.util.Map;

import com.jfinal.core.Controller;
import com.loan.model.Data;
import com.loan.model.Role;
import com.loan.service.RoleService;
import com.loan.util.Json;
 
/**
 * 权限控制器
 */
public class RoleController extends Controller {
	private static RoleService role = new RoleService();
	
	/**
	 *  获取权限列表
	 */
	public void list() {
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Map<String,String> params = Data.flat(getParaMap());
		renderJson(Json.ok("数据获取成功！", role.list(page, size, params)));
	}
	
	/**
	 * 编辑权限信息
	 */
	public void edit() {
		int id = getParaToInt("id",0);
		Role item = id>0 ? Role.dao.findById(id) : new Role();
		item.setName(getPara("name")); 
		item.setMenu(getPara("menu")); 
		item.setCode(getPara("code")); 
		item.setCid(getParaToInt("cid"));
		if(id>0 ? item.update() : item.setCreateTime(new Date()).save()){
			renderJson(Json.ok("信息编辑成功！")); 
		}else{
			renderJson(Json.error("信息编辑失败！"));
		}
	}
	
	/**
	 *  获取权限信息
	 */
	public void info() {
		renderJson(Json.ok("数据获取成功！", Role.dao.findById(getParaToInt("id",0))));
	}
	
	/**
	 * 权限删除
	 */
	public void del() {
		Role  r= Role.dao.findById(getParaToInt("id",0));
		if(r!=null){
			r.delete();
			renderJson(Json.ok("删除成功！"));
		}
	}
}
 